package com.jie.myvhr.controller;

import com.jie.myvhr.bean.ChatResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

import java.security.Principal;

/**
 * @author jie
 * @date 2019/6/29 -11:08
 */
@Controller
public class WsController {
    @Autowired
    SimpMessagingTemplate messagingTemplate;
    /**
     *  @ SendTo注解表示当服务器有消息需要推送的时候，
     *  会对订阅了@SendTo中路径的浏览器发送消息。
     *
     *  点对点
     */
    @MessageMapping("/ws/chat")
    public void handleChat(Principal principal, String msg) {
        String destUser = msg.substring(msg.lastIndexOf(";") + 1, msg.length());
        String message = msg.substring(0, msg.lastIndexOf(";"));
        messagingTemplate.convertAndSendToUser(destUser, "/queue/chat", new ChatResp(message, principal.getName()));
    }

    /**
     * 群发
     */
    @MessageMapping("/ws/nf")
    @SendTo("/topic/nf")
    public String handleNF() {
        System.out.println("235465>>>>>>>>>>>>>>>>>>>>>>>>>13243567");
        return "系统消息";
    }

}
